class BlackDeathInventoryManager extends InventoryManager
	config(Game);

struct Recipe
{
	var class<Inventory> Result;
	var array< Name >  Ingrediants;
};

var array< Recipe > Recipes;

var array < int > KnowRecipes;

simulated function bool AddInventory(Inventory NewItem, optional bool bDoNotActivate)
{
	local Inventory Item, LastItem;

	// The item should not have been destroyed if we get here.
	if( (NewItem != None) && !NewItem.bDeleteMe )
	{
		// if we don't have an inventory list, start here
		if( InventoryChain == None )
		{
			InventoryChain = newItem;
		}
		else
		{
			// Skip if already in the inventory
			for (Item = InventoryChain; Item != None; Item = Item.Inventory)
			{
				if( Item == NewItem )
				{
					return FALSE;
				}
				LastItem = Item;
			}
			LastItem.Inventory = NewItem;
		}

		`LogInv("adding" @ NewItem @ "bDoNotActivate:" @ bDoNotActivate);
		
		NewItem.SetOwner( Instigator );
		NewItem.Instigator = Instigator;
		NewItem.InvManager = Self;
		//`log("adding inventory"@NewItem);
		//NewItem.GivenTo( Instigator);
		
		
		if(BlackDeathWeapon(NewItem) != none)
		{
			//`log("attaching item");
			BlackDeathWeapon(NewItem).attachToHolder( Instigator.Mesh, BlackDeathWeapon(NewItem).HolderSocket);
		}

		// Trigger inventory event
		Instigator.TriggerEventClass(class'SeqEvent_GetInventory', NewItem);
		return TRUE;
	}

	return FALSE;
}

simulated function SwitchToBestWeapon( optional bool bForceADifferentWeapon )
{
	local Weapon BestWeapon;

	if (Instigator.IsLocallyControlled())
	{
		// if we don't already have a pending weapon,
		if( bForceADifferentWeapon ||
			PendingWeapon == None ||
			(AIController(Instigator.Controller) != None) )
		{
			// figure out the new weapon to bring up
			BestWeapon = GetBestWeapon( bForceADifferentWeapon );

			// if it matches our current weapon then don't bother switching
			if( BestWeapon == Instigator.Weapon )
			{
				BestWeapon = None;
				PendingWeapon = None;
				if (Instigator.Weapon != None)
				{
					Instigator.Weapon.Activate();
				}
			}
		}

		if (BestWeapon != None)
		{
			SetCurrentWeapon(BestWeapon);
		}
	}
}

simulated function SetItemsHidden(bool bIsVisible)
{
	local Inventory Inv;
	//`log("setting tiems hidden");
	if(InventoryChain != none)
	{
		Inv = InventoryChain;
		while(Inv != none)
		{
			//`log("Hideing item"@Inv);
			if(BlackDeathWeapon(Inv) != none)
				BlackDeathWeapon(Inv).ChangeVisibility(bIsVisible);
			Inv = Inv.Inventory;
		}
	}
}

simulated function Array< int > getKnowRecipes()
{
	return KnowRecipes;
}

simulated function bool Craft(Recipe recp)//Check before useing, can partially remove inventory and then fail
{
	local Name n;
	local inventory inv;
	foreach recp.Ingrediants(n)
	{
		inv = inventorychain;
		while(true)
		{
			if(inv == none)
			{
				return false;
			}
			if(inv.isA(n))
			{
				RemoveFromInventory(inv);
				break;
			}
			inv = inv.inventory;
		}
	}
	inv = CreateInventory(recp.Result);
	return (inv != none);
}

simulated function bool CheckRecipe(Array< inventory > items, out Recipe recp)
{
	local Recipe R;
	local Array<inventory> UsedItems;
	local Inventory inv, used;
	local bool Avalible;
	local int k;
	foreach Recipes(R)
	{
		for(k = 0; k < R.Ingrediants.length; k++)
		{
			useditems[k] = none;
			foreach items(inv)
			{
				if(inv.isA(R.Ingrediants[k]))
				{
					Avalible = true;
					foreach useditems(used)
					{
						if(used == inv)
						{
							Avalible = false;
							break;
						}
					}
					if(Avalible)
					{
						UsedItems[k] = inv;
						break;
					}
				}
			}
			if(UsedItems[k] == none)
				break;
			if((k+1) == R.Ingrediants.length)
			{
				recp = R;
				return true;
			}
		}
	}
	return false;
}

defaultproperties
{
	PendingFire(0)=0
	PendingFire(1)=0
}